package com.yanze.controller;

import com.yanze.pojo.Friendcircle;
import com.yanze.pojo.Friendcommit;
import com.yanze.pojo.Friendimage;
import com.yanze.pojo.User;
import com.yanze.pojo.request.ReqCommitFriend;
import com.yanze.pojo.request.Dynamics;
import com.yanze.pojo.response.DynamicBean;
import com.yanze.service.CommitFriendService;
import com.yanze.service.FriendCircleService;
import com.yanze.service.FriendImageService;
import com.yanze.service.UserService;
import com.yanze.utils.BaseJSONResult;
import com.yanze.utils.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("friend")
public class FriendController {

    @Autowired
    FriendCircleService mFriendCircleService;

    @Autowired
    FriendImageService mImageService;

    @Autowired
    UserService mUserservice;

    @Autowired
    CommitFriendService mCommitService;

    /**
     * 发布动态
     */
    @RequestMapping(path = "/publishDynamics", method = RequestMethod.POST)
    public BaseJSONResult publishDynamics(@RequestBody Dynamics dynamics) {
        if (dynamics == null) {
            return BaseJSONResult.errorMsg("参数错误");
        }
        Calendar calendar = Calendar.getInstance();
        Long friendid = calendar.getTime().getTime();
        Friendcircle friendcircle = new Friendcircle();
        friendcircle.setFriendid(friendid);
        friendcircle.setTitle(dynamics.getTitle());
        friendcircle.setContent(dynamics.getContent());
        friendcircle.setTime(new Date());
        friendcircle.setUserId(dynamics.getUserid());
        int flag = mFriendCircleService.publishDynamics(friendcircle);//添加动态
        List<String> images = dynamics.getImages();
        if (images.size() == 1) {
            Friendimage one = new Friendimage();
            one.setFriendid(friendid);
            one.setTime(new Date());
            one.setPath(images.get(0));
            mImageService.insertImage(one);
        } else {
            List<Friendimage> friendimages = new ArrayList<>();
            for (int i = 0; i < images.size(); i++) {
                Friendimage image = new Friendimage();
                image.setFriendid(friendid);
                image.setTime(new Date());
                image.setPath(images.get(i));
                friendimages.add(image);
            }
            mImageService.insertImages(friendimages);
        }
        if (flag > 0) {
            return BaseJSONResult.ok(friendcircle);
        } else {
            return BaseJSONResult.errorMsg(C.errmsg.insertError);
        }
    }

    /**
     * 获取所有动态
     */
    @RequestMapping(path = "/getDynamics/{page}/{pageSize}", method = RequestMethod.GET)
    public BaseJSONResult getAllDynamics(@PathVariable int page, @PathVariable int pageSize) {
        if (pageSize < 1) {
            pageSize = 1;
        }
        if (page < 1) {
            page = 1;
        }

        List<Friendcircle> dynamics = mFriendCircleService.getAllDynamics(page, pageSize);//获取朋友圈动态
        if (dynamics == null || dynamics.isEmpty()) {
            return BaseJSONResult.ok(dynamics);
        }
        List<DynamicBean> respDynamic = getDynamicByObject(dynamics);
        return BaseJSONResult.ok(respDynamic);
    }

    /**
     * 获取个人所有动态
     */
    @RequestMapping(path = "/getDynamicsById/{userid}/{page}/{pageSize}", method = RequestMethod.GET)
    public BaseJSONResult getDynamicsById(@PathVariable("userid") String userid,
                                          @PathVariable("page") int page,
                                          @PathVariable("pageSize") int pageSize) {
        if (pageSize < 1) {
            pageSize = 1;
        }
        if (page < 1) {
            page = 1;
        }
        List<Friendcircle> dynamics = mFriendCircleService.getUserDynamics(userid, page, pageSize);
        if (dynamics == null || dynamics.isEmpty()) {
            return BaseJSONResult.ok(dynamics);
        }
        List<DynamicBean> respDynamic = getDynamicByObject(dynamics);
        return BaseJSONResult.ok(respDynamic);
    }

    /**
     * 删除个人动态
     */
    @RequestMapping(path = "/deleteDynamics", method = RequestMethod.POST)
    public BaseJSONResult deleteDynamics(@RequestParam Long friendid) {
        int flag = mFriendCircleService.deleteDynamicById(friendid);// java中的int类型最大值是2147483647 存储长度为32bit.所以范围是“-2^32”到“2^32-1”; 也就是“-2147483648”到“2147483647”;
        if (flag > 0) {
            return BaseJSONResult.ok();
        } else {
            return BaseJSONResult.errorMsg(C.errmsg.deleteError);
        }
    }

    private List<DynamicBean> getDynamicByObject(List<Friendcircle> dynamics) {
        List<DynamicBean> respDynamic = new ArrayList<>();
        for (int i = 0; i < dynamics.size(); i++) {
            User user = mUserservice.queryUserById(dynamics.get(i).getUserId());//获取用户信息
            String header;
            String nickname;
            String userid;
            if (user != null) {
                header = user.getUsericon();
                nickname = user.getNickname();
                userid = user.getUserid();
            } else {
                header = "Header/0454f443-b9f6-4d8f-bf41-7e7634265ece.jpg";
                nickname = "匿名用户";
                userid = "";
            }

            List<Friendimage> friendimages = mImageService.queryImageByFid(dynamics.get(i).getFriendid());//获取动态对应的图片
            List<String> images = new ArrayList<>();
            for (int j = 0; j < friendimages.size(); j++) {
                images.add(friendimages.get(j).getPath());
            }
            DynamicBean tempBean = new DynamicBean(dynamics.get(i), images, header, nickname, userid);
            respDynamic.add(tempBean);
        }
        return respDynamic;
    }

    /**
     * 发表评论
     */
    @RequestMapping(path = "/publishCommit", method = RequestMethod.POST)
    public BaseJSONResult publishCommit(@RequestBody ReqCommitFriend commit) {
        Calendar calendar = Calendar.getInstance();
        Long commitid = calendar.getTime().getTime();

        Friendcommit friendcommit = new Friendcommit();
        friendcommit.setCommitid(commitid);
        friendcommit.setTime(new Date());
        friendcommit.setContent(commit.getContent());
        friendcommit.setFindcircleid(commit.getFriendcircleid());
        friendcommit.setUserid(commit.getUserid());
        int flag = mCommitService.addFriendCommit(friendcommit);
        if (flag > 0) {
            return BaseJSONResult.ok();
        } else {
            return BaseJSONResult.errorMsg(C.errmsg.insertError);
        }
    }

    /**
     * 获取动态评论
     */
    @RequestMapping(path = "/getDynamicCommit/{dynamicId}", method = RequestMethod.GET)
    public BaseJSONResult getDynamicCommit(@PathVariable("dynamicId") String dynamicId) {
        List<Friendcommit> commits = mCommitService.getDynamicCommit(dynamicId);
        return BaseJSONResult.ok(commits);
    }

    /**
     * 删除评论
     */
    @RequestMapping(path = "/deleteCommit/{commitid}", method = RequestMethod.DELETE)
    public BaseJSONResult deleteCommit(@PathVariable("commitid") Long commitid) {
        int flag = mCommitService.deleteCommit(commitid);
        if (flag > 0) {
            return BaseJSONResult.ok();
        } else {
            return BaseJSONResult.errorMsg(C.errmsg.deleteError);
        }
    }


    /**
     * test
     */
    @RequestMapping(path = "/getImages/{fid}", method = RequestMethod.GET)
    public BaseJSONResult getImages(@PathVariable("fid") Long fid) {
        List<Friendimage> friendimages = mImageService.queryImageByFid(fid);
        if (friendimages == null || friendimages.isEmpty()) {
            return BaseJSONResult.errorMsg("参数错误");
        }
        List<String> images = new ArrayList<>();
        for (int i = 0; i < friendimages.size(); i++) {
            images.add(friendimages.get(i).getPath());
        }
        return BaseJSONResult.ok(friendimages);
    }

    /**
     * test2
     */
    @RequestMapping(path = "/getStrings", method = RequestMethod.GET)
    public BaseJSONResult getStrings() {
        List<String> strs = new ArrayList<>();
        strs.add("A://schoolinformation");
        strs.add("A://schoolinformation");
        strs.add("A://schoolinformation");
        return BaseJSONResult.ok(strs);
    }
}
